package com.kingdee.eas.fdc.customerservice.client;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;

import javax.swing.event.ChangeListener;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.ctrl.extendcontrols.KDBizPromptBox;
import com.kingdee.bos.ctrl.kdf.table.IRow;
import com.kingdee.bos.ctrl.kdf.table.KDTDefaultCellEditor;
import com.kingdee.bos.ctrl.kdf.util.editor.ICellEditor;
import com.kingdee.bos.ctrl.swing.event.DataChangeEvent;
import com.kingdee.bos.ctrl.swing.event.DataChangeListener;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.dao.query.SQLExecutorFactory;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.metadata.entity.SelectorItemInfo;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.fdc.crm.basedata.RoomFactory;
import com.kingdee.eas.fdc.crm.basedata.RoomInfo;
import com.kingdee.eas.fdc.crm.basedata.SellProjectInfo;
import com.kingdee.eas.fdc.propertymgmt.RoomCustomerSettingCollection;
import com.kingdee.eas.fdc.propertymgmt.RoomCustomerSettingFactory;
import com.kingdee.eas.fdc.propertymgmt.RoomCustomerSettingInfo;
import com.kingdee.eas.fdc.sellhouse.FDCCustomerFactory;
import com.kingdee.eas.fdc.sellhouse.FDCCustomerInfo;
import com.kingdee.eas.fdc.sellhouse.PurchaseCustomerInfoCollection;
import com.kingdee.eas.fdc.sellhouse.PurchaseCustomerInfoFactory;
import com.kingdee.eas.fdc.sellhouse.PurchaseCustomerInfoInfo;
import com.kingdee.eas.fdc.sellhouse.PurchaseFactory;
import com.kingdee.eas.fdc.sellhouse.PurchaseInfo;


public class CMSInnerCheckBillEditUICTEx extends CMSInnerCheckBillEditUI {

	public CMSInnerCheckBillEditUICTEx() throws Exception {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onLoad() throws Exception {
		// TODO Auto-generated method stub
		super.onLoad();
		//kdtRoomEntry.getColumn("owners").getStyleAttributes().setLocked(false);
		//kdtRoomEntry.getColumn("ownerPhone").getStyleAttributes().setLocked(false);
		addFilter();
	}
  public void addFilter() {

	  SellProjectInfo sellp = (SellProjectInfo) prmtSellProject.getValue();
	    EntityViewInfo evi = new EntityViewInfo();
		FilterInfo filterInfo = new FilterInfo();
		filterInfo.getFilterItems().add(new FilterItemInfo("sellProject.name", sellp.getName(),CompareType.EQUALS));
		if (null == sellp)
			filterInfo.getFilterItems().add(new FilterItemInfo("sellProject.name", editData.getSellProject().getName(), CompareType.EQUALS));
		   filterInfo.getFilterItems().add(new FilterItemInfo("roomCustomerSetting.isPayMan", true, CompareType.EQUALS));
		    //filterInfo.getFilterItems().add(new FilterItemInfo("sellState",datas.toString(), CompareType.INCLUDE));
		    evi.setFilter(filterInfo);
		final KDBizPromptBox prmtRoomnew= new KDBizPromptBox();
		prmtRoomnew.setQueryInfo("com.kingdee.eas.fdc.customerservice.app.RoomQuery");
		prmtRoomnew.setEntityViewInfo(evi);
		prmtRoomnew.setDisplayFormat("$name$");
		prmtRoomnew.setCommitFormat("$name$");
		prmtRoomnew.setEditFormat("$name$");
		prmtRoomnew.setRequired(true);
		prmtRoomnew.setEditable(false);

		KDTDefaultCellEditor editor = new KDTDefaultCellEditor(prmtRoomnew);
		kdtRoomEntry.getColumn("room").setEditor(editor);

	     prmtRoomnew.addDataChangeListener(new DataChangeListener() {

			public void dataChanged(DataChangeEvent eventObj) {
				try {
					showByRoomNameNew(prmtRoomnew);
				} catch (Exception e) {
					handleException(e);
				}
			}
		});
		
	}
  
  public void showByRoomNameNew(KDBizPromptBox prmtRoom) throws Exception{
	  String customerNames=null;
	  int rowIndex = kdtRoomEntry.getSelectManager().getActiveRowIndex();
		IRow row = kdtRoomEntry.getRow(rowIndex);
		if (null != prmtRoom.getData())
		{
			String roomId = ((RoomInfo)prmtRoom.getData()).getId().toString();
			RoomInfo room=RoomFactory.getRemoteInstance().getRoomInfo(new ObjectUuidPK(roomId));
		    EntityViewInfo evi = new EntityViewInfo();
			FilterInfo filterInfo = new FilterInfo();
			filterInfo.getFilterItems().add(new FilterItemInfo("room.id", room.getId().toString(), CompareType.EQUALS));
		    evi.setFilter(filterInfo);
		    SelectorItemCollection selectors = new SelectorItemCollection();
		    selectors.add(new SelectorItemInfo("*"));
		    selectors.add(new SelectorItemInfo("customer.*"));
		    selectors.add(new SelectorItemInfo("room.*"));
		    evi.setSelector(selectors);
			RoomCustomerSettingCollection roomSettingColl = RoomCustomerSettingFactory.getRemoteInstance().getRoomCustomerSettingCollection(evi);
			RoomCustomerSettingInfo roomSetingInfo=null;
			if(roomSettingColl==null)
				return;
			if(roomSettingColl.size()>0)
			{
				roomSetingInfo=roomSettingColl.get(0);
			}
				  customerNames = roomSetingInfo.getCustomer().getName();
							String builder = "";
							if (null != roomSetingInfo.getCustomer().getPhone()) {
								builder = roomSetingInfo.getCustomer().getPhone();
								if (null != roomSetingInfo.getCustomer().getTel()) {
									builder = builder + ";";
									builder = builder +roomSetingInfo.getCustomer().getTel();
								}
							} else if (null !=roomSetingInfo.getCustomer().getTel())
								builder = builder +roomSetingInfo.getCustomer().getTel();
							else
								builder = builder + "";
					row.getCell("ownerPhone").setValue(builder);
					row.getCell("owners").setValue(customerNames);  
				  if(roomSetingInfo.getRoom() !=null)
				  {
					  //RoomInfo roomInfo=RoomFactory.getRemoteInstance().getRoomInfo(new ObjectUuidPK(roomSetingInfo.getRoom().getId().toString()));
					  row.getCell("room").setValue( prmtRoom.getData()); 
					  
				  }	
		}
    }

  
  
  private FDCCustomerInfo getCustomerShowBySHERoomNew(String roomId)
	throws Exception {
	FDCCustomerInfo info;
	label0: {
		info = null;
		String queryStmt = CMSBaseDataUtil.getSHEFilterSQL(roomId);
		EntityViewInfo view = new EntityViewInfo();
		FilterInfo filterInfo = new FilterInfo();
		filterInfo.getFilterItems().add(
				new FilterItemInfo("id", queryStmt, CompareType.INNER));
		view.setFilter(filterInfo);
		SelectorItemCollection sic = new SelectorItemCollection();
		sic.add(new SelectorItemInfo("*"));
		sic.add(new SelectorItemInfo("customer.*"));
		view.setSelector(sic);
		PurchaseCustomerInfoCollection collection = PurchaseCustomerInfoFactory
				.getRemoteInstance()
				.getPurchaseCustomerInfoCollection(view);
		if (collection == null || collection.size() <= 0)
			break label0;
		int mainSeq = 0;
		Iterator iterator = collection.iterator();
		PurchaseCustomerInfoInfo pcInfo;
		do {
			if (!iterator.hasNext())
				break label0;
			pcInfo = (PurchaseCustomerInfoInfo) iterator.next();
		} while (pcInfo.getSeq() != mainSeq);
		info = pcInfo.getCustomer();
	}
	return info;
	}
  @Override
public void actionSubmit_actionPerformed(ActionEvent e) throws Exception {
	// TODO Auto-generated method stub
	super.actionSubmit_actionPerformed(e);
}
  
}
